

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text.RegularExpressions;
using JTC.MiniCommerce.Store;
using JTC.MiniCommerce.Localization;
using System.Globalization;

namespace JTC.MiniCommerce.Web {
  public class WebUtility {

    #region Methods

    #region Public

    /// <summary>
    /// Gets the name of the user.
    /// </summary>
    /// <returns></returns>
    public static string GetUserName() {
      string userName = string.Empty;
      if (HttpContext.Current.User.Identity.IsAuthenticated) {
        userName = HttpContext.Current.User.Identity.Name;
      }
      else {
        //we'll tag them with an anon userName until they register
        if (HttpContext.Current.Request.Cookies["MiniCommerce_ShopperId"] != null) {
          userName = HttpContext.Current.Request.Cookies["MiniCommerce_ShopperId"].Value;
        }
        else {
          //if we have never seen them, return the current anonymous ID for the user
          userName = HttpContext.Current.Profile.UserName;
        }
      }
      HttpContext.Current.Response.Cookies["MiniCommerce_ShopperId"].Value = userName;
      HttpContext.Current.Response.Cookies["MiniCommerce_ShopperId"].Expires = DateTime.Today.AddDays(365);
      return userName;
    }

    /// <summary>
    /// Sessions the id.
    /// </summary>
    /// <returns></returns>
    public static string SessionId() {
      return System.Web.HttpContext.Current.Session.SessionID;
    }

    /// <summary>
    /// Ensures the SSL.
    /// </summary>
    public static void EnsureSsl() {
      Uri currentUrl = HttpContext.Current.Request.Url;
      if(!currentUrl.IsLoopback) {//Don't check when in development mode (i.e. localhost)
        if(!currentUrl.Scheme.Equals("https", StringComparison.CurrentCultureIgnoreCase)) {
          UriBuilder uriBuilder = new UriBuilder(currentUrl);
          uriBuilder.Scheme = "https";
          uriBuilder.Port = -1;
          HttpContext.Current.Response.Redirect(uriBuilder.Uri.ToString(), true);
        }
      }
    }

    //Many thanks to Paul Ingles for this Code
    //http://www.codeproject.com/aspnet/creditcardvalidator.asp
    //Modified by Spook, 3/2006
    /// <summary>
    /// Determines whether [is valid card type] [the specified card number].
    /// </summary>
    /// <param name="cardNumber">The card number.</param>
    /// <param name="cardType">Type of the card.</param>
    /// <returns>
    /// 	<c>true</c> if [is valid card type] [the specified card number]; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsValidCardType(string cardNumber, CreditCardType cardType) {
      bool bOut = false;

      // AMEX -- 34 or 37 -- 15 length
      if((Regex.IsMatch(cardNumber, "^(34|37)"))
           && ((cardType == CreditCardType.Amex)))
        bOut = 15 == cardNumber.Length;


    // MasterCard -- 51 through 55 -- 16 length
      else if((Regex.IsMatch(cardNumber, "^(51|52|53|54|55)")) &&
                ((cardType == CreditCardType.MasterCard)))
        bOut = 16 == cardNumber.Length;

    // VISA -- 4 -- 13 and 16 length
      else if((Regex.IsMatch(cardNumber, "^(4)")) &&
                ((cardType == CreditCardType.VISA)))
        bOut = 13 == cardNumber.Length || 16 == cardNumber.Length;

    // Discover -- 6011 -- 16 length
      else if((Regex.IsMatch(cardNumber, "^(6011)")) &&
               ((cardType == CreditCardType.Discover)))
        bOut = 16 == cardNumber.Length;

      return bOut;
    }

    #endregion

    #endregion

  }
}
